<template>
  <div class="login">
    <div class="login-box">
      <div class="top">
        <div class="logo"><img src="./homepageStatic/logo.png"
               alt=""></div>
      </div>
      <div class="mid">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 status-icon>
          <el-form-item prop="mobile">
            <el-input class="info"
                      v-model="dataForm.mobile"
                      placeholder="手机号"></el-input>
          </el-form-item>
        <el-form-item
                      prop="checkCode">
          <el-input v-model="dataForm.checkCode"
                    placeholder="验证码">
            <span slot="append" style="border:0;color:#1F87E8;background-color:rgba(0,0,0,0);width:100%;height:100%;" v-if="!isPending" @click="getCheckCode">获取验证码</span>
            <span slot="append" style="border:0;color:#606266;background-color:rgba(0,0,0,0);width:100%;height:100%;" v-if="isPending">{{remainingTime}}</span>
          </el-input>
        </el-form-item>
          <el-form-item>
            <div class="item-btn">
              <input type="button"
                     value="登录"
                     @click="dataFormSubmit()" style="display: inline-block;width: 40%;margin:0 ;">
              <span style="margin-right:20px;"><a href="/login">账号密码登录</a></span>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="form-footer">
            <!-- <el-checkbox v-model="savePasswotd">三天内自动登录</el-checkbox> -->
              <span><a href="/registe">没有账号？请先注册</a></span>
            <span><a href="/resetPassword">忘记密码?</a></span>
            </div>
          </el-form-item>
        </el-form>
      </div>

      <div class="bottom">Copyright © 2024 彩味塔线上服务平台</div>
    </div>
  </div>
</template>

<script>
import { isMobile } from '@/utils/validate'
import { getUUID } from '@/utils'
export default {
  data () {

    var validatecheckCode = (rule, value, callback) => {
      if (this.checkcodeRunning) {
        return
      }
      this.checkcodeRunning = true
      if (this.codeIsLegal) {
        callback()
      }else{
        this.$http({
          url: this.$http.adornUrl('/p/sms/checkRetrieve'),
          method: 'get',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            code: this.dataForm.checkCode
          })
        }).then(({data}) =>{
          if (data == '000') {
            callback(new Error('检验次数次数过多，请稍后重试'))
          }else if (data == '111') {
            callback(new Error('验证码错误'))
          }else if (data == '222') {
            callback(new Error('验证码已过期'))
          }else if (data == '999'){
            this.codeIsLegal = true
            callback()
          }
        })
      }
      this.checkcodeRunning = false
    };


    var validateMobile = (rule,value,callback) =>{
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else{
        console.log('Running');
        // callback()
        this.$http({
          url: this.$http.adornUrl('/sys/user/checkUserByPhone'),
          method: 'post',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            // code: this.dataForm.checkCode
          })
        }).then(({data}) =>{
          console.log(data);
          if (data == false) {
            callback(new Error('该手机号未注册，不能用于登录'))
          }else{
            callback()
          }
          // if (data == '000') {
          //   callback(new Error('检验次数次数过多，请稍后重试'))
          // }else if (data == '111') {
          //   callback(new Error('验证码错误'))
          // }else if (data == '222') {
          //   callback(new Error('验证码已过期'))
          // }else if (data == '999'){
          //   this.codeIsLegal = true
            callback()
          // }
        })
      }
    }


    return {
      isPending: false,
      remainingTime:60,
      checkcodeRunning: false,
      dataForm: {
        mobile: '',
        checkCode: '',
        uuid: '',
        captcha: ''
      },
      dataRule: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile,  trigger: 'blur' }
        ],
        checkCode: [
          { required: true, message: '验证码', trigger: 'blur' },
          // { validator: validatecheckCode,  trigger: 'blur' },
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      captchaPath: ''
    }
  },
  created () {
    // this.getCaptcha()
    // var mobile = this.$cookie.get('mobile')
    // var checkCode = this.$cookie.get('checkCode')
    // if (mobile && checkCode) {
    //   this.mobile = mobile
    //   this.checkCode = checkCode
    // }
  },
  methods: {
    // 提交表单
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/login?grant_type=admin'),
            method: 'post',
            data: this.$http.adornData({
              'loginType': 2,
              'principal': this.dataForm.mobile,
              'imageCode': this.dataForm.checkCode
            })
          }).then(({ data }) => {
            if (this.savePasswotd) {
              this.$cookie.set('Authorization', 'bearer' + data.access_token,3)
              // this.$cookie.set('mobile', this.data.mobile,3)
              // this.$cookie.set('checkCode', this.data.checkCode,3)
            }else{
              this.$cookie.set('Authorization', 'bearer' + data.access_token)
            }
            this.$router.replace({ name: 'home' })
          }).catch(() => {
            // this.getCaptcha()
          })
        }
      })
    },

    getCheckCode(){
      if (!this.dataForm.mobile) {
        this.$message.error('请先输入手机号')
        return
      }
      this.sendMsg()
      this.isPending = true
      var conter = setInterval(() => {
        this.remainingTime-=1
      }, 1000);
      setTimeout(() =>{
        clearInterval(conter)
        this.isPending = false
        this.remainingTime = 60
      },60000)
    },

    sendMsg(){
      console.log('running');
      this.$http({
        url: this.$http.adornUrl('/p/sms/loginbySendSms'),
        method: 'post',
        params: this.$http.adornParams({
          mobile: this.dataForm.mobile
        })
      }).then((res) =>{
          console.log(res);
      }).catch(() =>{
        this.remainingTime = 60
        this.isPending = false
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/login-bg.png) no-repeat;
  background-size: cover;
  position: fixed;
}
.login .login-box {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 100%;
  padding-top: 10%;
}
.login .login-box .top {
  margin-bottom: 30px;
  text-align: center;
}
.login .login-box .top .logo {
  font-size: 0;
}
.login .login-box .top .company {
  font-size: 16px;
  margin-top: 10px;
}
.login .login-box .mid {
  font-size: 14px;
}
.login .login-box .mid .item-btn {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.login .login-box .mid .item-btn input {
  border: 0;
  width: 200px;
  height: 40px;
  box-shadow: 0;
  background: #1f87e8;
  color: #fff;
  border-radius: 3px;
}
.info {
  width: 410px;
}
.login-captcha {
  height: 40px;
}
.login .login-box .bottom {
  position: absolute;
  bottom: 10%;
  width: 100%;
  color: #999;
  font-size: 12px;
  text-align: center;
}
.form-footer{
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-right: 20px;
  // font-size: 10px;
  color: #B7B8B7;
}
.el-checkbox__label {
  color: #B7B8B7;
}
</style>
